package com.dzl.micro;

import com.dzl.admin.WelfareBaseInfoController;
import com.dzl.base.controller.BaseController;
import com.dzl.brand.pojo.WelfareBrand;
import com.dzl.constant.CommonConstants;
import com.dzl.goods.pojo.query.WelfareGoodsPageQuery;
import com.dzl.goods.pojo.vo.WelfareGoodsBaseVo;
import com.dzl.goods.pojo.vo.WelfareGoodsClassifyVo;
import com.dzl.goods.service.WelfareGoodsService;
import com.dzl.micro.pojo.WelfareMicroPage;
import com.dzl.micro.pojo.vo.WelfareMicroVo;
import com.dzl.micro.service.WelfareMicroPageService;
import com.dzl.util.EmptyUtils;
import com.dzl.util.JsonResult;
import com.dzl.util.PageUtils;
import com.dzl.util.RedisUtil;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 微页面表(WelfareMicroPage)表控制层
 * @author ChenLong
 * @date 2021-07-30 11:13:10
 */
@Slf4j
@RestController
@RequestMapping("/welfareMicroPage")
public class WelfareMicroPageController extends WelfareBaseInfoController {

    @Autowired
    private WelfareMicroPageService service;
    @Autowired
    private WelfareGoodsService welfareGoodsService;

    /**
     * 获取微页面装修列表
     * @author ChenLong
     * @date 2021-08-02 14:17:11
     * @param pageId 微页面ID
     * @return com.dzl.util.JsonResult<com.dzl.micro.pojo.vo.WelfareMicroVo>
     **/
    @GetMapping(value = "getMicroPageByPageId")
    public JsonResult<WelfareMicroVo> getMicroPageByPageId(Integer pageId) {
        log.info("getMicroPageByPageId in: " + pageId);
        if (EmptyUtils.isEmpty(pageId)) {
            if(pageId==0){
                pageId= getEnterpriseHomePageId();
            }else {
                pageId = 1; //默认首页
            }
        }
        Object object = RedisUtil.get("microPage:"+pageId);
        if(object != null){
            WelfareMicroVo welfareMicroVo = (WelfareMicroVo) RedisUtil.get("microPage"+pageId);
            return JsonResult.success(welfareMicroVo);
        }
        WelfareMicroVo welfareMicroVo = service.getMicroPageByPageId(pageId, CommonConstants.DATA_PORT.APPLET);
        RedisUtil.set("microPage:"+pageId, welfareMicroVo);
        return JsonResult.success(welfareMicroVo);
    }

    /**
     * 获取微页面分类
     * @author ChenLong
     * @date 2021-08-02 14:16:52
     * @param pageId 微页面ID
     * @return com.dzl.util.JsonResult<java.util.List<com.dzl.goods.pojo.vo.WelfareGoodsClassifyVo>>
     **/
    @GetMapping(value = "getGoodsClassifyForMicro")
    public JsonResult<List<WelfareGoodsClassifyVo>> getGoodsClassifyForMicro(Integer pageId) {
        log.info("getGoodsClassifyForMicro in: " + pageId);
        return JsonResult.success(service.getGoodsClassifyForMicro(pageId,getDataSourcesEnterpriseId()));
    }

    /**
     * 获取微页面品牌信息（通过微页面ID）
     * @author ChenLong
     * @date 2021-08-02 10:34:34
     * @param welfareGoodsQuery query实体
     * @return com.dzl.util.JsonResult<java.util.List<com.dzl.brand.pojo.WelfareBrand>>
     **/
    @GetMapping(value = "getBrandsByClassifyForMicro")
    public JsonResult<List<WelfareBrand>> getBrandsByClassifyForMicro(WelfareGoodsPageQuery welfareGoodsQuery) {
        log.info("getBrandsByClassifyForMicro welfareGoodsQuery: " + welfareGoodsQuery.toString());
        List<WelfareBrand> welfareBrandList = service.getBrandsByClassifyForMicro(welfareGoodsQuery);
        return JsonResult.success(welfareBrandList);
    }

    /**
     * 获取微页面商品列表信息（通过微页面ID）
     * @author ChenLong
     * @date 2021-08-02 10:34:39
     * @param welfareGoodsPageQuery query实体
     * @return com.dzl.util.PageUtils<com.dzl.goods.pojo.vo.WelfareGoodsBaseVo>
     **/
    @GetMapping(value = "getGoodsListByClassifyForMicro")
    public PageUtils<WelfareGoodsBaseVo> getGoodsListByClassifyForMicro(WelfareGoodsPageQuery welfareGoodsPageQuery) {
        log.info("getGoodsListByClassifyForMicro in welfareGoodsPageQuery: " + welfareGoodsPageQuery.toString());
        //List<WelfareGoodsBaseVo> list = service.getGoodsByClassifyForMicro(welfareGoodsPageQuery);
        PageInfo<WelfareGoodsBaseVo> pageInfo = welfareGoodsService.getGoodsListPageByMicroId(welfareGoodsPageQuery);
        return new PageUtils<>(pageInfo);
    }

    /**
     * 获取微页面跳转商品列表
     * @author ChenLong
     * @date 2021-08-02 14:16:17
     * @param welfareGoodsPageQuery query实体
     * @return com.dzl.util.PageUtils<com.dzl.goods.pojo.vo.WelfareGoodsBaseVo>
     **/
    @GetMapping(value = "getGoodsListLinkByMicroId")
    public PageUtils<WelfareGoodsBaseVo> getGoodsListLinkByMicroId(WelfareGoodsPageQuery welfareGoodsPageQuery) {
        log.info("getGoodsListLinkByMicroId in welfareGoodsPageQuery: " + welfareGoodsPageQuery.toString());
        PageInfo<WelfareGoodsBaseVo> pageInfo = welfareGoodsService.getGoodsListPageByMicroId(welfareGoodsPageQuery);
        return new PageUtils<>(pageInfo);
    }

    /**
     * 描述: 根据企业ID查询微页面首页pageId
     * @date 2022/2/21 14:41
     * @author wangchao
     * @Param
     * @return
    **/
    public Integer getEnterpriseHomePageId(){
        Integer pageId=null;
        //根据企业ID查询pageId
        WelfareMicroPage microPage=new WelfareMicroPage();
        microPage.setIsHome(CommonConstants.YES);
        microPage.setStatus(CommonConstants.YES);
        microPage.setEnterpriseId(getDataSourcesEnterpriseId());
        microPage=service.selectOne(microPage);
        if(microPage!=null){
            pageId=microPage.getId();
        }
        return pageId;
    }
}
